﻿// Author: AustralTek 
// Create date: 2010/05/04
// http://www.australtek.com
// Copyright: 2009 - 2010

(function () {
    function _DojoGrid(object, options) {
        try {

            var privateParameters = null;
            function _initialize(myDiv, options) {
                try {
                    privateParameters = new PrivateParameters(options);
                    privateParameters.Version = {};
                    privateParameters.Version.Namespace = 'IndustrialDashboard';
                    privateParameters.Version.info = 'ID 2.0';
                    privateParameters.Container = myDiv;
                    if (privateParameters.Events) {
                        if (privateParameters.Events.onError == undefined) privateParameters.Events.onError = function (error) { };
                    } else privateParameters.Events = {}; privateParameters.Events.onError = function (error) { alert(error); };
                } catch (Error) { privateParameters.Events.onError(Error); }
            }
            function _refreshData(parameters) {
                try {
                    var url = privateParameters.URL;
                    var options = {}; options.data = {};
                    options.data.DatabaseParameters = privateParameters.DatabaseParameters;
                    options.data.JSONFile = privateParameters.JSONFile;
                    for (var conParam in privateParameters.ConnectionParameters)
                        options[conParam] = privateParameters.ConnectionParameters[conParam];
                    if ((parameters) && (parameters.Version))
                        options.data.DatabaseParameters.QueryParameters = parameters.GetQueryParameters();
                    else options = (parameters) ? parameters : options;

                    options.data.Namespace = privateParameters.Version.Namespace;
                    options.data.Class = privateParameters.Version.Class;

                    options.onSuccess = function (requestData) {
                        if ((requestData) && (!requestData.message)) {
                            privateParameters.Container.innerHTML = '';
                            privateParameters.Data = requestData;
                            _redraw(privateParameters.Container, privateParameters.Data);
                        }
                        else {
                            privateParameters.Container.innerHTML = '';
                        }
                    };

                    options.onFailure = function (args) {
                        privateParameters.Events.onError(args);
                    }
                    RequestJSON(url, options);
                } catch (Error) { privateParameters.Events.onError(Error); }
            };
            function _redraw(container, rdata) {
                try {
                    var jsonStore;
                    dojo.addOnLoad(function () {
                        jsonStore = new dojo.data.ItemFileReadStore({ data: { items: rdata.Table} });
                        var layout = [];
                        jsonStore.fetch({ onComplete:
    		                function (items) {
    		                    for (property in items[0]) {
    		                        if (property.indexOf('_') != 0)
    		                            layout.push({ 'field': property, width: 'auto' });
    		                    }
    		                }
                        });
                        var grid = new dojox.grid.DataGrid({
                            store: jsonStore,
                            structure: layout
                        }, document.createElement('div'));
                        container.appendChild(grid.domNode);
                        grid.startup();
                    });
                } catch (Error) { privateParameters.Events.onError(Error); }
            };

            _initialize(object, options);

            /*------------------------*/
            /*---------Public---------*/
            /*------------------------*/
            this.GetDatabaseEngine = function () { return DatabaseConnection.GetDatabaseEngine(privateParameters.DatabaseEngine); };
            this.SetDatabaseEngine = function (newDatabaseEngine) { DatabaseConnection.SetDatabaseEngine(privateParameters, newDatabaseEngine); };
            this.GetConnectionStringName = function () { return DatabaseConnection.GetConnectionStringName(privateParameters.ConnectionStringName); };
            this.SetConnectionStringName = function (newConnectionStringName) { DatabaseConnection.SetConnectionStringName(privateParameters, newConnectionStringName); };
            this.GetTablesCount = function () { return DatabaseConnection.GetTablesCount(privateParameters.TablesCount); };
            this.SetTablesCount = function (newTablesCount) { DatabaseConnection.SetTablesCount(privateParameters, newTablesCount); };
            this.GetProcedure = function () { return DatabaseConnection.GetProcedure(privateParameters.Procedure); };
            this.SetProcedure = function (newProcedure) { DatabaseConnection.SetProcedure(privateParameters, newProcedure); };
            this.RefreshData = function (parameters) {
                _refreshData(parameters);
            };
        } catch (Error) { privateParameters.Events.onError(Error); }
    };
    window.DojoGrid = function (object, options) { return new _DojoGrid(object, options); }
})();